﻿using BaseDll;
using Newtonsoft.Json;
using OtherDevice;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using UserData;

namespace ModuleUnit
{
    public class Config_CameraClib
    {
        [Category("工站参数"), DisplayName("21.视觉控制"), Description("视觉控制")]
        public string str_VisionControl { set; get; } = "";
        [Category("工站参数"), DisplayName("22.上相机编号"), Description("上相机编号")]
        public int n_上相机编号 { set; get; } = 1;
        [Category("工站参数"), DisplayName("23.下相机编号"), Description("下相机编号")]
        public int n_下相机编号 { set; get; } = 2;
        [Category("工站参数"), DisplayName("26.上相机九点标定任务号"), Description("上相机九点标定任务号")]
        public string n_上相机九点标定任务号 { set; get; } = "";
        [Category("工站参数"), DisplayName("27.下相机九点标定任务号"), Description("下相机九点标定任务号")]
        public string n_下相机九点标定任务号 { set; get; } = "";
        [Category("工站参数"), DisplayName("28.上相机对位下相机任务号"), Description("上相机对位下相机任务号")]
        public string n_上相机对位下相机任务号 { set; get; } = "";
        [Category("工站参数"), DisplayName("29.下相机对位上相机任务号"), Description("下相机对位上相机任务号")]
        public string n_下相机对位上相机任务号 { set; get; } = "";
        [Category("工站参数"), DisplayName("30.下相机视野中旋转"), Description("下相机视野中旋转")]
        public bool b_下相机视野中旋转 { set; get; } = true;
        [Category("工站参数"), DisplayName("30.旋转标定任务号"), Description("旋转标定任务号")]
        public string n_旋转标定任务号 { set; get; } = "";
        [Category("工站参数"), DisplayName("18.光源控制器"), Description("光源控制器")]
        public string str_光源控制器 { set; get; } = "";
        [Category("工站参数"), DisplayName("19.上光源"), Description("上光源ID")]
        public int n_上光源编号 { set; get; } = 0;
        [Category("工站参数"), DisplayName("20.下光源"), Description("下光源ID")]
        public int n_下光源编号 { set; get; } = 1;


    }
    [Description("上下相机对位")]
    public class Module_上下相机对位 : ModuleBase
    {
        public Config_CameraClib config = null;
        public Module_上下相机对位(object config, string strName) : base(strName)
        {

            if (config == null)
            {
                this.config = new Config_CameraClib();
            }
            else
            {
                this.config = JsonConvert.DeserializeObject<Config_CameraClib>(config.ToString());

            }
            Vision_组装定位Mgr.GetInstance().AddVisionCoordinate(str_ModuleName);
            this.obj_Config = this.config;

        }
        LightCtrlBase Device_光源控制器
        {
            set { }
            get
            {
                return DeviceMgr.GetInstance().Function_获取串口(config.str_光源控制器) as LightCtrlBase;
            }
        }
        Vision_Base Device_视觉
        {
            set { }
            get
            {
                return DeviceMgr.GetInstance().Function_获取网口(config.str_VisionControl) as OtherDevice.Vision_Base;
            }
        }
        double[] VX = new double[9];
        double[] VY = new double[9];
        double[] MX = new double[9];
        double[] MY = new double[9];
        List<XYUPoint> list_SnapPoint = new List<XYUPoint>();
        List<XYUPoint> list_VisionPoint = new List<XYUPoint>();
        XYUPoint Point_上CCD = new XYUPoint();
        public void Function_SetUpLight(int n_Value)
        {
            Device_光源控制器?.Set(config.n_上光源编号, n_Value);
        }
        public void Function_SetDownLight(int n_Value)
        {
            Device_光源控制器?.Set(config.n_下光源编号, n_Value);
        }
        public bool Function_上相机九点标定(int n_Time,XYUPoint point_Snap,XYUPoint point_Center, ref XYUPoint point_Result)
        {
            if (n_Time >= 9)
                return false;
            string strMsg = "";
            if (config.n_上相机编号 < 0)
                strMsg = $"{config.n_上相机九点标定任务号}";
            else
                strMsg = $"{config.n_上相机编号},{config.n_上相机九点标定任务号}";


            string[] result = new string[] { };
            if (Device_视觉?.VisionControlUsing(strMsg, out result) == 0)
            {
                string[] ListValue = result;
                VX[n_Time] = Convert.ToDouble(ListValue[0]);
                VY[n_Time] = Convert.ToDouble(ListValue[1]);
                point_Result = new XYUPoint(VX[n_Time], VY[n_Time]);
                MX[n_Time] = point_Snap.x - point_Center.x;
                MY[n_Time] = point_Snap.y - point_Center.y;
                if (n_Time == 8)
                {
                    Vision_组装定位Mgr.GetInstance().setCameraCoordinate(str_ModuleName, CoordinateType.上CCD, VX, VY, MX, MY);
                    Vision_组装定位Mgr.GetInstance().Save();
                }
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool Function_下相机九点标定(int n_Time, XYUPoint point_Snap, XYUPoint point_Center, ref XYUPoint point_Result)
        {
            if (n_Time >= 9)
                return false;
            string strMsg = "";
            if (config.n_下相机编号 < 0)
                strMsg = $"{config.n_下相机九点标定任务号}";
            else
                strMsg = $"{config.n_下相机编号},{config.n_下相机九点标定任务号}";

            string[] result = new string[] { };
            if (Device_视觉?.VisionControlUsing(strMsg, out result) == 0)
            {
                string[] ListValue = result;
                VX[n_Time] = Convert.ToDouble(ListValue[0]);
                VY[n_Time] = Convert.ToDouble(ListValue[1]);
                point_Result = new XYUPoint(VX[n_Time], VY[n_Time]);
                MX[n_Time] = point_Snap.x - point_Center.x;
                MY[n_Time] = point_Snap.y - point_Center.y;
                if (n_Time == 8)
                {
                    Vision_组装定位Mgr.GetInstance().setCameraCoordinate(str_ModuleName, CoordinateType.下CCD, VX, VY, MX, MY);
                    Vision_组装定位Mgr.GetInstance().Save();
                }
            }
            else
            {
                return false;
            }
            return true;
        }
        public bool Function_上相机对位下相机(ref XYUPoint point_Result)
        {
            string[] result = new string[] { };
            string strMsg = "";
            if (config.n_上相机编号 < 0)
                strMsg = $"{config.n_上相机对位下相机任务号}";
            else
                strMsg = $"{config.n_上相机编号},{config.n_上相机对位下相机任务号}";

            if (Device_视觉?.VisionControlUsing(strMsg, out result) == 0)
            {
                Point_上CCD.x = result[0].ToDouble();
                Point_上CCD.y = result[1].ToDouble();
                Point_上CCD.u = result[2].ToDouble();
                point_Result = Point_上CCD;

                return true;

            }
            else
            {
                return false;
            }
        }
        public bool Function_下相机对位上相机(XYUPoint point_当前位置, ref XYUPoint point_Result)
        {
            string[] result = new string[] { };
            string strMsg = "";
            if (config.n_下相机编号 < 0)
                strMsg = $"{config.n_下相机对位上相机任务号}";
            else
                strMsg = $"{config.n_下相机编号},{config.n_下相机对位上相机任务号}";

            if (Device_视觉?.VisionControlUsing(strMsg, out result) == 0)
            {
                XYUPoint Point_下CCD = new XYUPoint();
                Point_下CCD.x = result[0].ToDouble();
                Point_下CCD.y = result[1].ToDouble();
                Point_下CCD.u = result[2].ToDouble();
                point_Result = Point_下CCD;
                Vision_组装定位Mgr.GetInstance().setVector_上CCD_下CCD(str_ModuleName, point_当前位置, Point_上CCD, Point_下CCD);
                Vision_组装定位Mgr.GetInstance().Save();
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool Function_旋转标定(int n_Time,XYUPoint point_Rotate,ref XYUPoint point_Result,bool b_End=false)
        {
            if(n_Time==0)
            {
                list_SnapPoint.Clear();
                list_VisionPoint.Clear();
            }
            string strMeg = "";
            if (config.b_下相机视野中旋转)
            {
                if (config.n_下相机编号 < 0)
                    strMeg = $"{config.n_旋转标定任务号}";
                else
                    strMeg = $"{config.n_下相机编号},{config.n_旋转标定任务号}";
            }
            else
            {
                if (config.n_上相机编号 < 0)
                    strMeg = $"{config.n_旋转标定任务号}";
                else
                    strMeg = $"{config.n_上相机编号},{config.n_旋转标定任务号}";
            }
            string[] result = new string[] { };
            if (Device_视觉?.VisionControlUsing(strMeg, out result) == 0)
            {
                XYUPoint point_Vision = new XYUPoint(Convert.ToDouble(result[0]), Convert.ToDouble(result[1]));
                point_Result = point_Vision;
                list_SnapPoint.Add(point_Rotate);
                list_VisionPoint.Add(point_Vision);
                if (b_End)
                {
                    XYUPoint[] Point_Snap = new XYUPoint[list_SnapPoint.Count];
                    XYUPoint[] Point_Vision = new XYUPoint[list_SnapPoint.Count];
                    for (int i = 0; i < list_SnapPoint.Count; i++)
                    {
                        Point_Snap[i] = list_SnapPoint[i];
                        Point_Vision[i] = list_VisionPoint[i];
                    }
                    Vision_组装定位Mgr.GetInstance().setRotateCenter(str_ModuleName, Point_Snap, Point_Vision);
                    Vision_组装定位Mgr.GetInstance().Save();
                }
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool Function_下相机定位产品(string n_TaskID, ref XYUPoint PointPix,Action actionMove=null)
        {
            string[] result = new string[] { };
            string strMsg = "";
            if (config.n_下相机编号 < 0)
                strMsg = $"{n_TaskID}";
            else
                strMsg = $"{config.n_下相机编号},{n_TaskID}";

            if (Device_视觉?.VisionControlUsing(strMsg, out result, actionMove) == 0)
            {
                PointPix.x = Convert.ToDouble(result[0]);
                PointPix.y = Convert.ToDouble(result[1]);
                PointPix.u = Convert.ToDouble(result[2]);
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool Function_上相机定位槽位(string n_TaskID, ref XYUPoint PointPix, Action actionMove = null)
        {
            string[] result = new string[] { };
            string strMsg = "";
            if (config.n_上相机编号 < 0)
                strMsg = $"{n_TaskID}";
            else
                strMsg = $"{config.n_上相机编号},{n_TaskID}";
            if (Device_视觉?.VisionControlUsing(strMsg, out result, actionMove) == 0)
            {
                PointPix.x = Convert.ToDouble(result[0]);
                PointPix.y = Convert.ToDouble(result[1]);
                PointPix.u = Convert.ToDouble(result[2]);
                return true;
            }
            else
            {
                return false;
            }

        }
        public XYUPoint Function_获取最终对位数据(XYUPoint point_UpCamSnap, XYUPoint point_UpCamPix, 
                                                  XYUPoint point_DownCamSnap, XYUPoint point_DownCamPix,bool b_Rotate)
        {
            Vision_组装定位Mgr.GetInstance().setVector_上CCD_槽位(str_ModuleName, point_UpCamSnap, point_UpCamPix);
            Vision_组装定位Mgr.GetInstance().setVector_下CCD_物料(str_ModuleName, point_DownCamSnap, point_DownCamPix);
            //旋转参数  后续添加
            Vision_组装定位Mgr.GetInstance().get组装目标位置(str_ModuleName, out XYUPoint pointTagert, b_Rotate);
            if (config.b_下相机视野中旋转 && b_Rotate)
                pointTagert.u = point_DownCamSnap.u + pointTagert.u;
            else
                pointTagert.u = point_DownCamSnap.u;
            return pointTagert;
        }

        public XYUPoint Function_获取最终对位数据(XYUPoint point_UpCamSnap, XYUPoint point_UpCamPix,
                                          XYUPoint point_DownCamSnap, XYUPoint point_DownCamPix, bool b_Rotate,XYUPoint point_DiffModule)
        {
            Vision_组装定位Mgr.GetInstance().setVector_上CCD_槽位(str_ModuleName, point_UpCamSnap, point_UpCamPix);
            Vision_组装定位Mgr.GetInstance().setVector_下CCD_物料(str_ModuleName, point_DownCamSnap, point_DownCamPix);
            //旋转参数  后续添加
            Vision_组装定位Mgr.GetInstance().get组装目标位置(str_ModuleName, out XYUPoint pointTagert, b_Rotate, point_DiffModule);
            if (config.b_下相机视野中旋转 && b_Rotate)
                pointTagert.u = point_DiffModule.u + pointTagert.u;
            else
                pointTagert.u = point_DownCamSnap.u;
            return pointTagert;
        }

        public XYUPoint Function_获取最终对位数据(XYUPoint point_UpCamSnap, XYUPoint point_UpCamPix,
                                                  XYUPoint point_夹爪_上相机, bool b_Rotate)
        {
            Vision_组装定位Mgr.GetInstance().setVector_上CCD_槽位(str_ModuleName, point_UpCamSnap, point_UpCamPix);
            Vision_组装定位Mgr.GetInstance().get组装目标位置(str_ModuleName, point_夹爪_上相机, out XYUPoint pointTagert);
            
            return pointTagert;
        }
        public bool Function_上相机曝光值设定(int n_Value)
        {
            if (Device_视觉?.VisionExposureChange($"{config.n_上相机编号},{n_Value}") == true)
                return true;
            else
            {
                return false;
            }
        }
        public bool Function_下相机曝光值设定(int n_Value)
        {
            if (Device_视觉?.VisionExposureChange($"{config.n_下相机编号},{n_Value}") == true)
                return true;
            else
            {
                return false;
            }
        }
        public bool Function_上相机功能扩展(string n_TaskID,Action action, ref string[] result)
        {
            string strMsg = "";
            if (config.n_上相机编号 < 0)
                strMsg = $"{n_TaskID}";
            else
                strMsg = $"{config.n_上相机编号},{n_TaskID}";
            if (Device_视觉?.VisionControlUsing(strMsg, out result, action) == 0)
                return true;
            else
            {
                return false;
            }
        }
        public bool Function_下相机功能扩展(string n_TaskID, Action action,ref string[] result)
        {
            string strMsg = "";
            if (config.n_下相机编号 < 0)
                strMsg = $"{n_TaskID}";
            else
                strMsg = $"{config.n_下相机编号},{n_TaskID}";
            if (Device_视觉?.VisionControlUsing(strMsg, out result, action) == 0)
                return true;
            else
            {
                return false;
            }
        }
        public XYUPoint Function_上相机坐标转换(XYUPoint snapMachinePos, XYUPoint VisionPix上CCD)
        {
            return Vision_组装定位Mgr.GetInstance().Function_上相机坐标转换(str_ModuleName, snapMachinePos, VisionPix上CCD);
        }
        public XYUPoint Function_下相机坐标转换( XYUPoint snapMachinePos, XYUPoint VisionPix下CCD)
        {
            return Vision_组装定位Mgr.GetInstance().Function_下相机坐标转换(str_ModuleName, snapMachinePos, VisionPix下CCD);
        }
    }
}